﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DatabaseAccess;
using DataModel;

namespace GlobalUtility
{
    public class GlobalUtils
    {
        public static void UpdateCableValidStatus(byte id, bool status)
        {
            ShortConnection sc = new ShortConnection(GlobalVar.ConnectionString);

            //这里和外面的区别是，将SQL的语句的编写，填写到这里。
            StringBuilder sb = new StringBuilder();
            sb.Append("UPDATE temperature_sensor SET");
            sb.Append(" isvalid = " + status);
            sb.Append(" WHERE");
            sb.Append(" id = " + id);
            sc.ExcuteNonQuery(sb.ToString());
        }

        //public static void GetTempratureData()
        //{
        //    ShortConnection sc = new ShortConnection(GlobalVar.ConnectionString);

        //    //这里和外面的区别是，将SQL的语句的编写，填写到这里。
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append("SELECT id, isvalid FROM temperature_sensor");
           
        //    DataTable dt = sc.getDataTable(sb.ToString());
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        byte id = Convert.ToByte(dr["id"]);
        //        bool isEnable = Convert.ToBoolean(dr["isvalid"]);
        //        TempratureModel gPST = new TempratureModel(id)
        //        {
        //            IsEnable = isEnable
        //        };
        //        GlobalVar.tempCache.AddOrUpdate(id, gPST, (key, value) => { return value = gPST; });
        //    }
        //}

        public static void UpdateCableData(byte id, TempratureModel model)
        {
            ShortConnection sc = new ShortConnection(GlobalVar.ConnectionString);

            //这里和外面的区别是，将SQL的语句的编写，填写到这里。
            StringBuilder sb = new StringBuilder();
            sb.Append("UPDATE temperature_sensor SET");

            for (int i = 1; i <= model.Values.Count; i++)
            {
                sb.Append(" value" + i + " = " + model.Values[i-1]+",");
            }

            sb.Append(" max = " + model.Max+",");
            sb.Append(" avg = " + model.Average+",");
            sb.Append(" created_time = '" + model.UpdatedTime+"',");
            sb.Append(" isvalid = " + model.IsValid);
            sb.Append(" WHERE");
            sb.Append(" id = " + id);
            sc.ExcuteNonQuery(sb.ToString());
        }
    }
}
